//
//  JQButton.m
//  SheShiDu
//
//  Created by 单剑秋 on 16/6/16.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "JQButton.h"

@implementation JQButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        // 设置字体颜色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 字体字号
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        // 设置两张图片
        [self setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"up"] forState:UIControlStateSelected];
        
        //        [self sizeToFit];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.titleLabel.frame.origin.x > self.imageView.frame.origin.x) {
        // 交换图片和文字的位置
        CGRect tempTitleLabelRect = self.titleLabel.frame;
        tempTitleLabelRect.origin.x = self.frame.size.width / 2 - (self.titleLabel.frame.size.width + self.imageView.frame.size.width) / 2 ;
        self.titleLabel.frame = tempTitleLabelRect;
        
        CGRect tempImageViewRect = self.imageView.frame;
        tempImageViewRect.origin.x = self.titleLabel.frame.size.width + self.titleLabel.frame.origin.x;
        self.imageView.frame = tempImageViewRect;
    }
    
    
}

- (void)buttonTypeChange
{
    self.selected = !self.selected;
}

@end
